<?php
/**
 * store 后台 肤质检测数据
 *
 * Author: hezhizheng
 * Date: 2018/5/10
 * Created by Panxsoft.
 */

namespace App\Http\Controllers\Admin\Store;

use App\Http\Controllers\ApiController;
use App\Http\Middleware\StoreAuth;
use App\Http\Requests\Wechat\SkinRecordIndexRequest;
use App\Http\Requests\Wechat\SkinRecordListRequest;
use App\Models\OpenWechat\WechatUser;
use App\Models\Store\SkinRecord;
use App\Models\Store\Store;
use App\Services\OpenWechat\WechatUserRepository;
use App\Services\Presenters\Admin\HmSkinRecordPresenter;
use App\Services\Presenters\Admin\WaxSkinRecordPresenter;
use App\Services\Repositories\SkinRecord\SkinRecordRepository;
use App\Services\Utils\Guard;

class SkinRecordController extends ApiController
{
    const ROUTES = [
        'list'             => 'api.admin.store.skin-record.list',
        'showPieChartData' => 'api.admin.store.skin-record.show-pie-chart-data',
        'indexUsageCount'  => 'api.admin.store.skin-record.index-usage-count',
    ];

    protected $skinRecordRepository;
    protected $wechatUserRepository;

    public function __construct(SkinRecordRepository $skinRecordRepository,
                                WechatUserRepository $wechatUserRepository)
    {
        $this->skinRecordRepository = $skinRecordRepository;
        $this->wechatUserRepository = $wechatUserRepository;
    }

    /**
     * 后台店铺,肤质检测数据管理,测肤统计数据
     * @test
     *
     * @return array|\Illuminate\Http\Response
     */
    public function indexUsageCount()
    {
        $store = Guard::storeAdmin()->store;

        $usage_count_info = $this->skinRecordRepository->storesUsageCountInfo([$store->id]);

        return $this->response($usage_count_info);
    }

    /**
     * 后台店铺,肤质检测数据管理,肤质检测数据的展示(详情)
     * @test
     *
     * @param SkinRecordListRequest $request
     *
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function skinRecordList(SkinRecordListRequest $request)
    {
        /** @var WechatUser $wechat_user */
        $wechat_user = WechatUser::findByOpenId($request->openid);

        $this->checkItemExist($wechat_user,trans('app.wechat_user_error'));

        $store_ids = $wechat_user->skinRecords->pluck('store_id')->all();

        $skin_detector_id = (int) $request->skin_detector_id;

        $skin_suggests = $this->skinRecordRepository->getCustomerSkinSuggests($skin_detector_id, Guard::store()->customer_id);

        $skin_record_ids = $this->skinRecordRepository->getSkinRecordIdsForList($skin_detector_id, $wechat_user);

        $skin_records = $this->skinRecordRepository->normalizeSkinRecordList($request, $wechat_user, $skin_record_ids, $store_ids, $skin_suggests);

        return $this->response(compact('skin_records'));

    }

    /**
     * 后台店铺,肤质检测数据管理,饼图展示hm、wax测肤统计数据
     * @test
     *
     * @return array|\Illuminate\Http\Response
     */
    public function showPieChartData()
    {
        $store = Guard::storeAdmin()->store;

        $store_ids = [$store->id];
        $customer_id = $store->customer_id;

        $hm_skin = HmSkinRecordPresenter::storesUsersStatisticLabelGroupData($store_ids, $customer_id);

        $wax_skin = WaxSkinRecordPresenter::storesUsersStatisticLevelGroupData($store_ids, $customer_id);

        return $this->response(compact('hm_skin', 'wax_skin'));
    }

}
